package org.grow.bill.config;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: xwg
 * @CreateDate: 2025/10/13
 */
@Component
//openfeign携带token
public class FeignReqInterceptor implements RequestInterceptor {
//    @Autowired
//    private HttpServletRequest httpServletRequest;

    @Override
    public void apply(RequestTemplate requestTemplate) {
//        RequestTemplate对象表示要进行远程调用发送的请求
        RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
        if(attributes instanceof  ServletRequestAttributes){
            ServletRequestAttributes  reqA = (ServletRequestAttributes) attributes;
            HttpServletRequest httpServletRequest = reqA.getRequest();
            String authorization = httpServletRequest.getHeader("Authorization");
            if (authorization != null) {
                requestTemplate.header("Authorization", authorization);
            }
        }

//        requestTemplate.header("Token", "sssyy.jwt.xxxx");
    }
}
